//
//  ZHParentController.swift
//  ZHPageView
//
//  Created by Rango on 2020/8/28.
//  Copyright © 2020 Rango. All rights reserved.
//

import UIKit

class ZHParentController: UIViewController {

    var segmentIndex: Int = 0 {
        didSet {
            switch segmentIndex {
            case 1:
                style.isShowIndicator = false
                style.isGradientTitleColor = true
                style.isScaleTitle = true
            case 2:
                style.isShowIndicator = false
                style.isGradientTitleColor = true
                style.isScaleTitle = true
                style.isShowExtraBtn = true
            case 3:
                style.isShowIndicator = false
                style.isScaleTitle = true
                style.isGradientTitleColor = true
                style.isContentScrollEnabled = false
                style.isContentPushAnimaEnabled = false
            case 4:
                style.isShowIndicator = false
                style.isGradientTitleColor = true
                style.isScaleTitle = true
                style.isShowExtraBtn = true
                style.defaultIndex = 1
                style.extraBtnTitle = "切换"
                style.isShowIndicator = true
            case 5:
                style.isShowIndicator = false
                style.isShowExtraBtn = true
                style.extraBtnTitle = "重载"
                style.isShowIndicator = true
                style.indicatorType = .line
            case 100:
                style.defaultIndex = 2
                style.isContentPushAnimaEnabled = false
                style.isScaleTitle = true
                style.isShowIndicator = true
            case 101:
                style.defaultIndex = 3
                style.isGradientTitleColor = true
                style.isShowIndicator = true
                style.isIndicatorAutoWidth = false
                style.indicatorWidth = 20
            case 102:
                style.isGradientTitleColor = true
                //style.isScaleTitle = true
                style.isShowIndicator = true
                style.isIndicatorAutoWidth = true
                style.indicatorType = .cover
                style.indicatorHeight = 28
                style.indicatorPadding = (style.height - style.indicatorHeight) / 2
                style.indicatorColor = .black
                style.indicatorCornerRadius = style.indicatorHeight / 2
                //style.isIndicatorBorder = true
            case 103:
                style.isGradientTitleColor = true
                //style.isScaleTitle = true
                style.isShowIndicator = true
                style.isIndicatorAutoWidth = true
                style.indicatorType = .cover
                style.indicatorHeight = 28
                style.indicatorPadding = (style.height - style.indicatorHeight) / 2
                style.indicatorColor = .clear
                style.indicatorCornerRadius = style.indicatorHeight / 2
                style.isIndicatorBorder = true
            case 104:
                style.isShowIndicator = true
                style.indicatorType = .custom
                style.indicatorCustomView = indicatorCustomView // 继承自 UIView 即可
                style.isIndicatorAutoWidth = false
                style.indicatorWidth = 12
                style.indicatorHeight = 10
                style.indicatorPadding = 2
                style.indicatorColor = .clear
            case 105: // 多层级嵌套
                style.isScaleTitle = true
                style.titleScale = 1.1
                style.isGradientTitleColor = true
                style.titleFont = UIFont.systemFont(ofSize: 16, weight: .medium)
                style.isShowShadow = false
                style.isShowBoundaryLine = false
                break
            default:
                break
            }
        }
    }
    private var defaultIndex: Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
    }

    func setUpUI() {
        view.backgroundColor = .white
        if segmentIndex == 105 {
            view.addSubview(multiPageView)
        } else {
            view.addSubview(pageView)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if segmentIndex == 105 {
            let multiPageViewX: CGFloat = 0
            let multiPageViewY: CGFloat = kStatusBarHeight() + kNavigationBarHeight(self)
            let multiPageViewW: CGFloat = kScreenWidth()
            let multiPageViewH: CGFloat = kScreenHeight() - multiPageViewY
            multiPageView.frame = CGRect(x: multiPageViewX, y: multiPageViewY, width: multiPageViewW, height: multiPageViewH)
        } else {
            let pageViewX: CGFloat = 0
            let pageViewY: CGFloat = kStatusBarHeight() + kNavigationBarHeight(self)
            let pageViewW: CGFloat = kScreenWidth()
            let pageViewH: CGFloat = kScreenHeight() - pageViewY
            pageView.frame = CGRect(x: pageViewX, y: pageViewY, width: pageViewW, height: pageViewH)
        }

    }
    
    deinit {
        print("\(#file)-\(#line)-\(#function)--->销毁")
    }
    
    private lazy var indicatorCustomView: ZHIndicatorCustomView = {
        return $0
    }(ZHIndicatorCustomView())

    private lazy var childVces: [UIViewController] = {
        let vc1 = ZHExampleViewController()
        vc1.title = "国际"
        let vc2 = ZHExampleTableController()
        vc2.title = "国内"
        let vc3 = ZHExampleViewController()
        vc3.title = "游戏频道"
        let vc4 = ZHExampleTableController()
        vc4.title = "体育"
        let vc5 = ZHExampleViewController()
        vc5.title = "情感专栏"
        let vc6 = ZHExampleTableController()
        vc6.title = "科技"
        let vc7 = ZHExampleViewController()
        vc7.title = "娱乐八卦"
        let vc8 = ZHExampleTableController()
        vc8.title = "纪录片"
        return [vc1, vc2, vc3, vc4, vc5, vc6, vc7, vc8]
    }()
    private lazy var titles: [String] = {
        return childVces.map { $0.title! }
    }()

    private lazy var newChildVces: [UIViewController] = {
        let vc1 = ZHExampleViewController()
        vc1.title = "关 注"
        let vc2 = ZHExampleTableController()
        vc2.title = "头条"
        let vc3 = ZHExampleViewController()
        vc3.title = "热 门"
        let vc4 = ZHExampleTableController()
        vc4.title = "PC"
        let vc5 = ZHExampleViewController()
        vc5.title = "主 机"
        let vc6 = ZHExampleTableController()
        vc6.title = "科技"
        let vc7 = ZHExampleViewController()
        vc7.title = "影 视"
        let vc8 = ZHExampleTableController()
        vc8.title = "硬件"
        let vc9 = ZHExampleViewController()
        vc9.title = "动 漫"
        let vc10 = ZHExampleTableController()
        vc10.title = "娱乐"
        
        return [vc1, vc2, vc3, vc4, vc5, vc6, vc7, vc8, vc9, vc10]
    }()
    private lazy var newTitles: [String] = {
        return newChildVces.map { $0.title! }
    }()

    private lazy var style: ZHSegmentStyle = {
        return $0
    }(ZHSegmentStyle())
    private lazy var pageView: ZHPageView = {
        $0.didSelectedIndexClosure = { [weak self] index in
            // 最终的 index
            guard let strongSelf = self else { return }
           
            if strongSelf.segmentIndex == 5 {
                print(strongSelf.newTitles[index])
            } else {
                if index % 2 == 0 {
                    if let vc = strongSelf.childVces[index] as? ZHExampleViewController {
                        vc.index = index
                    }
                } else {
                    if let vc = strongSelf.childVces[index] as? ZHExampleTableController {
                        vc.index = index
                    }
                }
            }
        }
        $0.didExtraBtnActionClosure = { [weak self] item in
            guard let strongSelf = self else { return }
            if strongSelf.segmentIndex == 4 {
                let index = arc4random_uniform(UInt32(strongSelf.childVces.count)-1)
                strongSelf.pageView.resetSelectedIndex(Int(index), animated: true)
            } else if strongSelf.segmentIndex == 5 {
                let defaultIndex = arc4random_uniform(UInt32(strongSelf.newChildVces.count)-1)
                strongSelf.pageView.reloadPageView(with: strongSelf.newTitles, childVces: strongSelf.newChildVces, defaultIndex: Int(defaultIndex), animated: true)
            }
        }
        return $0
    }(ZHPageView(frame: .zero, style: style, titles: titles, childVces: childVces, parent: self))


    // MARK: - ---> Multi
    private lazy var multiChildVces: [UIViewController] = {
        let vc1 = ZHExampleMultiController1()
        vc1.title = "影视"
        let vc2 = ZHExampleTableController()
        vc2.title = "动漫"
        let vc3 = ZHExampleMultiController2()
        vc3.title = "游戏"
        return [vc1, vc2, vc3]
    }()
    private lazy var multiTitles: [String] = {
        return multiChildVces.map { $0.title! }
    }()

    private lazy var multiPageView: ZHPageView = {
        $0.didSelectedIndexClosure = { [weak self] index in
            print("didSelectedIndexClosure---\(index)")
        }
        $0.didExtraBtnActionClosure = { [weak self] item in
            guard let strongSelf = self else { return }

        }
        return $0
    }(ZHPageView(frame: .zero, style: style, titles: multiTitles, childVces: multiChildVces, parent: self))
}
